package com.shu.mmall.controller.portal;

import com.shu.mmall.common.Const;
import com.shu.mmall.common.ResponseCode;
import com.shu.mmall.common.Result;
import com.shu.mmall.pojo.User;
import com.shu.mmall.service.IUserService;
import com.shu.mmall.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author shujinhao
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    IUserService userService;

    /**
     * 登录
     * @param username 用户名
     * @param password 密码
     * @param session 会话
     * @return json
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> login(String username, String password, HttpSession session){
        Result<User> result = userService.login(username, password);
        //如果成功把用户放入会话中
        if (result.isSuccess()) {
            session.setAttribute(Const.CURRENT_USER, result.getData());
        }
        return result;
    }

    /**
     * 登出
     * @param session 会话
     * @return 结果
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return Result.createBySuccess();
    }

    /**
     * 注册
     * @param user 用户
     * @return 结果
     */
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public Result register(User user){
        return userService.register(user);
    }

    /**
     * 校验
     * @param str 校验内容
     * @param type 校验类型
     * @return 结果
     */
    @RequestMapping(value = "checkValid.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> checkValid(String str,String type){
        return userService.checkValid(str,type);
    }

    /**
     * 获取用户信息
     * @param session 会话
     * @return 结果
     */
    @RequestMapping(value = "getUserInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> getUserInfo(HttpSession session){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorMessage("用户未登录，无法获取当前用户的信息");
        }
        return Result.createBySuccess(user);
    }

    /**
     * 查询问题
     * @param username 用户名
     * @return 结果
     */
    @RequestMapping(value = "forgetGetQuestion.do",method = RequestMethod.POST)
    @ResponseBody
    public Result forgetGetQuestion(String username){
        return userService.selectQuestion(username);
    }

    /**
     * 校验答案
     * @param username 用户名
     * @param question 问题
     * @param answer 答案
     * @return 结果
     */
    @RequestMapping(value = "forgetCheckAnswer.do",method = RequestMethod.POST)
    @ResponseBody
    public Result forgetCheckAnswer(String username,String question,String answer){
        return userService.checkAnswer(username,question,answer);
    }

    /**
     * 密码重置
     * @param username 用户名
     * @param passwordNew 新密码
     * @param forgetToken token
     * @return 结果
     */
    @RequestMapping(value = "forgetRestPassword.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> forgetRestPassword(String username,String passwordNew,String forgetToken){
        return userService.forgetRestPassword(username,passwordNew,forgetToken);
    }

    /**
     * 在线重置密码
     * @param session 会话
     * @param passwordOld 老密码
     * @param passwordNew 新密码
     * @return 结果
     */
    @RequestMapping(value = "resetPassword.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<String> resetPassword(HttpSession session,String passwordOld,String passwordNew){
        User user=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(user)){
            return Result.createByErrorMessage("用户未登录，无法获取当前用户的信息");
        }
        return userService.resetPassword(passwordOld,passwordNew,user);
    }
    @RequestMapping(value = "updateUserInfo.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> updateUserInfo(HttpSession session,User user){
        User currentUser=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(currentUser)){
            return Result.createByErrorMessage("用户未登录，无法获取当前用户的信息");
        }
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        Result<User> result = userService.updateUserInfo(user);
        //如果更新成功，更新session
        if (result.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,result.getData());
        }
        return result;
    }
    @RequestMapping(value = "getInformation.do",method = RequestMethod.POST)
    @ResponseBody
    public Result<User> getInformation(HttpSession session){
        User currentUser=(User)session.getAttribute(Const.CURRENT_USER);
        if (EmptyUtil.isNull(currentUser)){
            return Result.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return userService.getInformation(currentUser.getId());
    }
}
